package graphics;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.util.Log;

public class BackgroundScreen {

	private static String TAG = BackgroundScreen.class.getSimpleName();

	/*
	 * bitmap = the image used as the background
	 * rectSrc = the subset of the image to draw on the canvas
	 * rectDest = the rectangle that the image will be scaled/translated to fit into
	 * screenWidth = the width of the view
	 * screenHeigth = the height of the view
	 */
	protected Bitmap bitmap;
	protected Rect rectSrc;
	protected Rect rectDest;
	protected int screenWidth;
	protected int screenHeight;

	public BackgroundScreen(Bitmap bitmap, int screenWidth, int screenHeight){
		Log.d(TAG, "BG Constructor");
		this.bitmap = bitmap;
		this.screenWidth = screenWidth;
		this.screenHeight = screenHeight;
		this.rectSrc = new Rect(0, 0, screenWidth, this.bitmap.getHeight());
		this.rectDest = new Rect(0, 0, screenWidth, screenHeight);
	}


	public void update(){
		/*
		 * Scroll-speed of the background
		 */
		rectSrc.offset(4, 0);
		/*
		 * If the subset has reach the end, reset it to the start of the bitmap
		 * (When the left side of the subset reaches 2120 px the subset is equal to
		 * the starting subset)
		 */
		if(rectSrc.right >= 2120+screenWidth){
			rectSrc.left = 0;
			rectSrc.right = screenWidth;
		}

	}

	public void draw(Canvas canvas) {
		try {
			canvas.drawBitmap(bitmap,rectSrc,rectDest,null);
		} catch (Exception e) {
			// Do nothing
		}
	}

}
